﻿
using Dapper;

using HBase;
using hCommand;
using System.Collections.Generic;
using System.Linq;

namespace Persistence.abs
{
    public abstract class AbsBatchServer : AbsWMSBaseServer, IBatchServer
    {
        public Page<Batch> QueryBatchPage<Batch>(BatchDto dto)
        {
         
            
            string sql = "SELECT DISTINCT w.* FROM core_item c JOIN wh_batch w on c.id=w.item_Id WHERE 1=1"; 
            //"select * from wh_batch where 1=1 ";

            if (!string.IsNullOrEmpty(dto.batchNo))
            {
                dto.batchNo = "%" + dto.batchNo + "%";
                sql += " AND batch_No like @batchNo ";
            }
            if (!string.IsNullOrEmpty(dto.itemName))
            {
                dto.itemName = "%" + dto.itemName + "%";
                sql += " AND item_Name like @itemName ";
            }
            if (dto.whAreaId != 0)
            {
                sql += $" and  c.classify_Id={dto.whAreaId}";
            }
            if (dto.id != 0)
            {
                sql += " AND id = @id ";
            }
            if (dto.itemId != 0)
            {
                sql += $" AND c.code like '%{dto.itemId}%' ";
            }
            if (dto.type != 0)
            {
                sql += " AND type = @type ";
            }
            if (dto.codes != null && dto.codes.Count > 0) {

                sql += " AND code in @codes";
            }


            return this.queryPage<Batch>(sql, "id", dto);
        }





        public bool UpdatePriority(WhBatch model)
        {
            string sql = @"UPDATE wh_batch SET Priority =@priority  WHERE id=@id";
            int a = opConn().Execute(sql, new { id = model.id, priority = model.priority });
            if (a >= 1)
            {
                return true;
            }
            return false;
        }

        public List<BatchExcelDto> GetExportList(BatchDto dto)
        {
            string sql = @"SELECT b.id,i.code,i.name,i.gg,b.count,i.unit FROM core_item i JOIN wh_batch b WHERE i.id=b.item_Id";
            if (dto.whAreaId != 0)
            {
                sql += $"  AND i.classify_Id={dto.whAreaId}";
            }
            List<BatchExcelDto> batchExcelDtos = readConn().Query<BatchExcelDto>(sql).ToList();

            return batchExcelDtos;

        }
    }
}
